<?php

Raise::load('core.net.http.RaiseHttpStream');

/**
 * TestCoreRaiseHttpStream class
 * Testing unit of 'phpraise/core/RaiseHttpStream.php'
 *
 * @author Sam-Mauris Yong / hellclanner at live dot com
 * @license http://www.opensource.org/licenses/bsd-license New BSD License
 * @package Raise.UnitTest.Test
 */
class TestCoreRaiseHttpStream extends RaiseTest {

    public static function testStream(){

        self::assertClassExists('RaiseHttpStream');
        self::assertClassExtends('RaiseHttpStream', 'RaiseStream');
        $request = RaiseHttpRequest::loadCurrent();
        self::assertObjectClass($request, 'RaiseHttpRequest');

        try{

            $stream = new RaiseHttpStream('localhost', 80);
            self::assertObjectClass($stream, 'RaiseHttpStream');

            self::assertTrue($stream->isReadable());
            self::assertTrue($stream->isWriteable());
            self::assertFalse($stream->isSeekable());

            $request = new RaiseHttpRequest();
            $request->headers()->add('Host', 'localhost', true);
            $request->uri('/');
            $request->version(RaiseHttpRequest::HTTP_1_1);
            $request->method(RaiseHttpRequest::METHOD_GET);
            $request->headers()->add('Connection', 'Keep-Alive', true);
            $request->headers()->add('User-Agent', 'Testing', true);
            $request->headers()->add('Accept', 'text/html, application/xhtml+xml, application/xml, text/plain, */*', true);
            $request->headers()->add('Accept-Language', 'en-us, en;q=0.5', true);
            $request->headers()->add('Accept-Charset', 'utf-8, *', true);

            self::assertStrictEqual($stream->write($request->__toString()), strlen($request->__toString()));

            $buff = '';
            while(true){
                $s = $stream->read(1024);
                $buff .= $s;
                if(strlen($s) < 1024){
                    break;
                }
            }

            self::addMessage($buff);

            $stream->close();
        }catch(Exception $x){
            self::addWarning('RaiseHttpStream testing did not run because no webserver is not running at "' . $request->headers()->get('Host') . '" or an error occurred.');
            self::addError($x->getMessage());
        }

    }


    public static function testBadHttpStream(){
        try{
            $stream = new RaiseHttpStream('127.34.203.1', 8080);
            self::assertObjectClass($stream, 'RaiseHttpStream');

            self::assertTrue($stream->isReadable());
            self::assertTrue($stream->isWriteable());
            self::assertFalse($stream->isSeekable());

            $request = new RaiseHttpRequest();
            $request->headers()->add('Host', 'Dead@#1', true);
            $request->uri('/');
            $request->version(RaiseHttpRequest::HTTP_1_1);
            $request->method(RaiseHttpRequest::METHOD_GET);
            $request->headers()->add('Connection', 'Closed', true);
            $request->headers()->add('User-Agent', 'Testing', true);
            $request->headers()->add('Accept', 'text/html, application/xhtml+xml, application/xml, text/plain, */*', true);
            $request->headers()->add('Accept-Language', 'en-us, en;q=0.5', true);
            $request->headers()->add('Accept-Charset', 'utf-8, *', true);

            self::assertStrictEqual($stream->write($request->__toString()), strlen($request->__toString()));

            $buff = '';
            while($s = $stream->read(1024)){
                $buff .= $s;
            }
            self::addMessage($buff);

            $stream->close();
            self::assertFalse($stream);
        }catch(Exception $x){
            self::assert($x);
        }
    }

}

